<?php

namespace App\Http\Controllers\Api\Admin;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Builder;
use App\Http\Resources\Api\Admin\AdminUsersResource;
use App\Http\Queries\AdminUserQuery;
use App\Models\Admin\AdminUsers;
use App\Models\Enum\Admin\AdminUserEnum;
use App\Models\Admin\AdminUsersIp;
use App\Models\Admin\AdminUsersRole;
use App\Http\Requests\Api\Admin\AdminUserStatusRequest;
use App\Http\Requests\Api\Admin\AdminUserIpTypeRequest;
use App\Http\Requests\Api\Admin\AdminUserSetRoleRequest;
use App\Http\Requests\Api\Admin\AdminUserResetPwRequest;
use App\Http\Controllers\Api\Controller;

class AdminUserManageController extends Controller
{
    /**
     * 根据职位获取用户
     * @param Request $request
     * @return mixed
     */
    public function getUser(Request $request)
    {
        $select = [
            'user_id',
            'name',
        ];
        $querys = AdminUsers::select($select)->where(['status' => AdminUserEnum::NORMAL]);

        if ($request->has('role_id')){
            $withMap = ['admin_role.role_id' => $request->input('role_id')];
            $querys = $querys->whereHas('roles', function (Builder $query) use ($withMap) {
               $query->where($withMap);
            })->get()->toArray();
        }

        return $this->success($querys);
    }

    //返回用户列表
    public function index(AdminUserQuery $query)
    {
        $users = $query->paginate($this->getPageSize());

        return AdminUsersResource::collection($users);
    }

    /**
     * 设置用户状态
     * @param AdminUserStatusRequest $request
     * @return mixed
     */
    public function setStatus(AdminUserStatusRequest $request)
    {
        $user_id = $request->input('user_id');
        $status = $request->input('status');
        if ($status != AdminUserEnum::NORMAL && $status !=AdminUserEnum::FREEZE){
            $status = -1;
        }
        $user = AdminUsers::find($user_id);
        if ($user){
            $user->status = $status;
            $user->save();
            return $this->success('设置成功');
        }

        return $this->failed('设置发生错误');
    }

    /**
     * 设置用户的登录ip
     * @param AdminUserIpTypeRequest $request
     * @return mixed
     */
    public function setIpType(AdminUserIpTypeRequest $request)
    {
        $ui_id = $request->input('ui_id');
        $type = $request->input('type');

        $ip = AdminUsersIp::find($ui_id);
        if ($ip){
            $ip->type = $type;
            $ip->save();
            return $this->success('设置成功');
        }
        return $this->failed('设置发生错误');
    }

    /**
     * 重置用户的密码
     * @param AdminUserResetPwRequest $request
     * @return mixed
     */
    public function resetPw(AdminUserResetPwRequest $request)
    {
        $user_id = $request->input('user_id');
        $newPw = $request->input('newPassword');

        $user = AdminUsers::find($user_id);
        if ($user){
            $user->password = $newPw;
            $user->save();
            return $this->success('重置成功');
        }

        return $this->failed('重置发生错误');
    }

    /**
     * 设置角色
     * @param AdminUserSetRoleRequest $request
     * @return mixed
     */
    public function setRole(AdminUserSetRoleRequest $request)
    {
        $user_id = $request->input('user_id');
        $role_id = $request->input('role_id');
        $status = $request->input('status');
        $own_power = $request->input('own_power');
        $data = ['role_id' => $role_id, 'user_id' => $user_id];
        $userRole = AdminUsersRole::where($data)->first();
        if ($status < 1){
            $userRole && $userRole->delete();
        }else{
            $data['own_power'] = $own_power;
            $userRole ? $userRole->update($data) : AdminUsersRole::create($data);
        }

        return $this->success('设置成功');
    }
}
